<template>
    <div id="login">
        <el-form id="loginForm" :model="loginData" :rules="loginRule"
                 ref="loginForm">
            <div id="loginTitle">{{logoText}}</div>
            <el-form-item prop="uname">
                <el-input v-model="loginData.uname" placeholder="工号"/>
            </el-form-item>
            <el-form-item prop="upass">
                <el-input v-model="loginData.upass" placeholder="密码" show-password/>
            </el-form-item>
            <el-button id="loginSubmit" type="primary" @click="login_submit('loginForm')">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    import {rsa_encrypt} from "@/plugins/helper";

    export default {
        name: "Login",
        computed: {
            logoText() {
                return this.$store.state.logo_text;
            }
        },
        data() {
            return {
                loginData: {
                    uname: 'admin',
                    upass: '123456'
                },
                loginRule: {
                    uname: [
                        {required: true, message: '请输入帐号', trigger: 'blur'}
                    ],
                    upass: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            login_submit(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.axios.get('/login', {params:{scope:'getkey'}})
                            .then(res => {
                                if (res.data.code === '0x00010') {
                                    let encrypt_data = rsa_encrypt(JSON.stringify(this.loginData), res.data.data);
                                    this.axios.get('/login', {params:{scope: 'login', enc_data: encrypt_data}})
                                        .then(res => {
                                            if (res.data.code === '0x00005') {
                                                this.$message.success(res.data.msg);
                                                this.$router.push('/');
                                            } else {
                                                this.$message.error(res.data.msg)
                                            }
                                        })
                                        .catch(err => {
                                            this.$message.error('系统错误：' + err);
                                        })

                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            })
                            .catch(err => {
                                this.$message.error('系统错误：' + err);
                            })
                    }
                })
            }
        }
    }
</script>

<style scoped lang="stylus">
    #loginForm {
        width 300px
        height 200px
        margin 0 auto
        position relative
        top 'calc(100vh / %s)' % 4
    }

    #loginSubmit {
        width 100%
    }

    #loginTitle {
        text-align center
        height 80px
        line-height 80px
        font-size 2.5em
    }
</style>